// scripts/data.js
let tableData = JSON.parse(localStorage.getItem('tableData')) || [];

function loadDataTable() {
    const contentArea = document.getElementById('content-area');
    contentArea.innerHTML = `
        <h2>数据管理</h2>
        <div class="form-container">
            <form id="data-form">
                <div class="form-group">
                    <label>姓名:</label>
                    <input type="text" id="name" required>
                </div>
                <div class="form-group">
                    <label>年龄:</label>
                    <input type="number" id="age" required>
                </div>
                <div class="form-group">
                    <label>兴趣领域:</label>
                    <input type="text" id="category" required>
                </div>
                <button type="submit">添加数据</button>
            </form>
        </div>
        <div class="table-container">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>姓名</th>
                        <th>年龄</th>
                        <th>兴趣领域</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="data-body"></tbody>
            </table>
        </div>
    `;

    // 初始化表格数据
    renderTable();
    initForm();
}

function renderTable() {
    const tbody = document.getElementById('data-body');
    tbody.innerHTML = '';
    
    tableData.forEach((item, index) => {
        const row = document.createElement('tr');
        row.innerHTML = `
            <td>${item.id}</td>
            <td>${item.name}</td>
            <td>${item.age}</td>
            <td>${item.category}</td>
            <td>
                <button onclick="editData(${index})">编辑</button>
                <button onclick="deleteData(${index})">删除</button>
            </td>
        `;
        tbody.appendChild(row);
    });
}

function initForm() {
    const form = document.getElementById('data-form');
    
    form.addEventListener('submit', (e) => {
        e.preventDefault();
        const newData = {
            id: tableData.length + 1,
            name: document.getElementById('name').value,
            age: parseInt(document.getElementById('age').value),
            category: document.getElementById('category').value
        };
        
        tableData.push(newData);
        localStorage.setItem('tableData', JSON.stringify(tableData));
        renderTable();
        form.reset();
    });
}

function deleteData(index) {
    tableData.splice(index, 1);
    localStorage.setItem('tableData', JSON.stringify(tableData));
    renderTable();
}

function editData(index) {
    const data = tableData[index];
    document.getElementById('name').value = data.name;
    document.getElementById('age').value = data.age;
    document.getElementById('category').value = data.category;

    document.getElementById('data-form').onsubmit = (e) => {
        e.preventDefault();
        tableData[index] = {
            id: data.id,
            name: document.getElementById('name').value,
            age: parseInt(document.getElementById('age').value),
            category: document.getElementById('category').value
        };
        localStorage.setItem('tableData', JSON.stringify(tableData));
        renderTable();
        document.getElementById('data-form').reset();
        document.getElementById('data-form').onsubmit = initForm();
    };
}
